﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using SoonCode.Core;
using SoonCode.FocusTest.Api;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.Views;
using MessageBox = HandyControl.Controls.MessageBox;

namespace SoonCode.FocusTest.ViewModels
{
    class ArchiveWindowViewModel:BindableBase
    {
        private ObservableCollection<MemberInfo> members;
        private ObservableCollection<MemberInfo> _selectedMember=new ObservableCollection<MemberInfo>();
        private IDialogService dialogService;
        private ObservableCollection<DictModel> statusList;
        private ObservableCollection<ClassesInfo> classesList;
        private int classesSelect = 0;

        
        public Visibility ClassVisibility { get; set; } = GlobalModel.ShowClass() ? Visibility.Visible : Visibility.Collapsed;


        public ObservableCollection<ClassesInfo> ClassesList
        {
            get { return classesList; }
            set
            {
                classesList = value;
                RaisePropertyChanged();
            }
        }

        public int ClassesSelect
        {
            get { return classesSelect; }
            set
            {
                classesSelect = value;
                RaisePropertyChanged();

            }
        }
        public ObservableCollection<MemberInfo> SelectedMember
        {
            get { return _selectedMember; }
            set
            {
                SetProperty(ref _selectedMember, value);
            }

        }
        public ObservableCollection<MemberInfo> Members
        {
            get { return members; }
            set
            {
                members = value;
                RaisePropertyChanged();
            }
        }

        public string Classes { get; set; }
        public string SearchChannel { get; set; }
        public string SearchSales { get; set; }
        public string SearchMember { get; set; }
        public string SearchName { get; set; }
        public string SearchStatus { get; set; }
        public ObservableCollection<DictModel> StatusList
        {
            get { return statusList; }
            set
            {
                statusList = value;
                RaisePropertyChanged();
            }
        }

        public ICommand OpenMemberEdit { get; set; }
        public ICommand DeleteMemberCommand { get; set; }
        public ICommand ModifyMemberCommand { get; set; }
        public ICommand SearchMemberCommand { get; set; }
        public ICommand ShowFeeCommand { get; set; }
        public ICommand ReloadDataCommand { get; set; }
        public ArchiveWindowViewModel(IDialogService service)
        {
            Task.Run(GetMembers);
            Task.Run(getStatuses);
            dialogService= service;
            OpenMemberEdit = new DelegateCommand(()=>ShowMemberEdit(null));
            ModifyMemberCommand = new DelegateCommand<string>(ShowMemberEdit);
            DeleteMemberCommand=new DelegateCommand<string>(DeleteMember);
            SearchMemberCommand = new DelegateCommand(DoSearchMember);
            ShowFeeCommand = new DelegateCommand<string>(ShowMemberFee);
            ReloadDataCommand = new DelegateCommand(() => { GetMembers(); });
            ClassesList = GlobalModel.Instance.ClassesList.DeepClone();
            ClassesList.Insert(0,new ClassesInfo());
        }

        private async void ShowMemberFee(string p)
        {
            DialogParameters param=new DialogParameters();
            param.Add("memberId",p);
            dialogService.ShowDialog(nameof(MemberFeeListView), param, async (res) =>
            {
                await Task.Run(GetMembers);
            }, "myDialogWindow");
        }
        private async void DoSearchMember()
        {
            GetMembers();
        }
        private async void getStatuses()
        {
            var list= await API.GetStatuses();
            list.Insert(0,new DictModel(){id = "",name="全部"});
            StatusList=list;
        }
        private async void DeleteMember(string p)
        {
            if (MessageBox.Show("确定要删除数据吗？", "系统提示", MessageBoxButton.YesNo, MessageBoxImage.Question) ==
                MessageBoxResult.Yes)
            {
                bool res = await API.DeleteMember(p);
                await Task.Run(GetMembers);
            }
        }
        private void ShowMemberEdit(string p)
        {
            DialogParameters param= null;
            if (p != null)
            {
                MemberInfo info = Members.FirstOrDefault(f => f.id == p);
                param=new DialogParameters();
                param.Add("member", info);
            }
            dialogService.ShowDialog(nameof(MemberEditView), param,async (res) =>
            {
                if (res.Result == ButtonResult.OK)
                {
                    await Task.Run(GetMembers);
                }
            }, "myDialogWindow");

        }
        private async void GetMembers()
        {
            try
            {
                Dictionary<string, object> where = new Dictionary<string, object>();
                where.Add("channel", SearchChannel);
                where.Add("name", SearchName);
                where.Add("salesPerson", SearchSales);
                where.Add("member_no", SearchMember);
                where.Add("statusId", SearchStatus);
                where.Add("classId", Classes);
                var list = await API.GetMemberList(where);
                list = new ObservableCollection<MemberInfo>(list.OrderByDescending(s => s.member_no));
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].index = i;
                }

                Task.Delay(500);
                Application.Current.Dispatcher.Invoke(() =>
                {
                    this.Members = list;
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
